﻿using System.Collections.Generic;
using System.Data;
using System.Data.Common;

namespace MaQiu.SqlExecute.DAL
{
    internal class StoreProcedure
    {
        private readonly SqlQuery _sql;

        public StoreProcedure(string name, DbParameter[] param)
        {
            _sql = new SqlQuery(name, param);
            _sql.GetCommand().CommandType = System.Data.CommandType.StoredProcedure;
        }

        public StoreProcedure(string name) : this(name, null)
        {
        }

        public DbCommand GetCommand()
        {
            return _sql.GetCommand();
        }

        public static StoreProcedure Create(string name, object param)
        {
            return new StoreProcedure(name, DbHepler.GetParameters(param));
        }

        public static StoreProcedure Create(string name)
        {
            return new StoreProcedure(name, null);
        }

        public DataTable GetDataTable()
        {
            return _sql.GetDataTable();
        }

        public int ExecuteNonQuery()
        {
            return _sql.ExecuteNonQuery();
        }

        public T ExecuteScalar<T>()
        {
            return _sql.ExecuteScalar<T>();
        }

        public List<T> ExecuteScalarList<T>()
        {
            return _sql.ExecuteScalarList<T>();
        }

        public T ToSingle<T>() where T : class, new()
        {
            return _sql.ToSingle<T>();
        }

        public List<T> ToList<T>() where T : class, new()
        {
            return _sql.ToList<T>();
        }
    }
}
